home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / lu62 / debug / aplcopy.c < prev    next >
C/C++ Source or Header  |  1996-07-10  |  19KB  |  596 lines

  1. /*************************************************************************/
  2. /*                APLCOPY  - copy function for APL                       */
  3. /*
  4.  *  CopyRight 1995. Nicholas Poljakov all rights reserved.
  5.  */
  6. /*************************************************************************/
  7.  
  8.  
  9. #include <dos.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <io.h>
  13. #include <fcntl.h>
  14. #include <stdlib.h>
  15. #include <state1.h>
  16. #define MAX_PATH 40
  17. #define ESC '\033'
  18.  
  19. extern char lu_id[8];
  20. struct find_t find;
  21. extern *pa[2];
  22. extern char tp_id[8];
  23. extern unsigned  long conv_id;
  24. extern char tp_exist;
  25. extern struct  part {            /* partner structure */
  26.           char     plu[8] ;         /* str plu_name        */
  27.                   int psl;                    /* str plu_s_limit     */
  28.           char     mode_name[8] ;         /* str mode_name        */
  29.                   int max_ru_size;            /* str ru_h_size       */
  30.                   int pacing;                 /* str =               */
  31.                   unsigned char lu_type;
  32.                }  pstr[4];
  33.  
  34.  
  35. /**************************************************************************/
  36. /*      RcvFile - receive file from remote LU.                            */
  37. /**************************************************************************/
  38.  
  39.     RcvFile ( char *p , int from)
  40.  {
  41.     unsigned char rts;
  42.     unsigned char wr;
  43.     unsigned int max_length ;
  44.     unsigned  char tpn[64];
  45.     struct rc {
  46.                 unsigned int prim;
  47.                 unsigned long sec;
  48.               } v_rc;
  49.     unsigned long rc;
  50.     char p_lu[9];
  51.     char p_mode[9];
  52.     int length;
  53.  
  54.               if (!tp_exist) {
  55.                                 tp_started
  56.                                             (lu_id,
  57.                                              tp_id,
  58.                                              &rc);
  59.                                 if (rc != 0) {
  60.                                                 return (-1);
  61.                                               }
  62.                                 tp_exist = 1; /* TP_started O.K! */
  63.                              }
  64.  
  65.               strcpy (tpn,"RCV "); /* name of work */
  66.               strcat (tpn, p );      /* name of file */
  67.               /*conv_id - return param -id work link */
  68.               strcpy(p_lu, pstr[from-1].plu);
  69.               strcpy(p_mode, pstr[from-1].mode_name);
  70.                    allocate  (p_lu,
  71.                               p_mode,
  72.                               tpn,
  73.                               WHEN_SESSION_ALLOCATED,
  74.                               NONE,
  75.                               NONE,
  76.                               NULL,
  77.                               NULL,
  78.                               tp_id,
  79.                               &conv_id,
  80.                               NULL,
  81.                               &v_rc) ;
  82.  
  83.  
  84.                   if (v_rc.prim != 0) {
  85.                                           return (-1);
  86.                                        }
  87.  
  88.             rcv(p-5);
  89.      return 0;
  90.  }
  91.  
  92.  /**************************************************************************/
  93.  /*          RmtCopy - copy file between remote LUs                        */
  94.  /**************************************************************************/
  95.    RmtCopy (char *p, int from, int to)
  96.  {
  97.       int ch;
  98.  
  99.  
  100.            window(14);
  101.            window_xy(14, 1, 4);
  102.            window_puts(14, "Function not supported");
  103.            while ((ch = getch()) != ESC) ;
  104.            deactivate(14);
  105.            return 0;
  106.  }
  107.  /**************************************************************************/
  108.  /*          CpyFile - copy file into directories of one LU                */
  109.  /**************************************************************************/
  110.    CpyFile(p)
  111.         char *p; {
  112.     long FS; /* File Size */
  113.     int fod;
  114.     int fid;
  115.     char *buff; /* input buffer */
  116.     char fsiz[10];
  117.     unsigned s;
  118.     long count;
  119.     unsigned len;
  120.     int ch, i;
  121.     char pt[80];
  122.  
  123.  
  124.     if(( fid = open(p, O_RDONLY | O_BINARY)) < 0){
  125.         window(20);
  126.         window_xy(20, 6, 1);
  127.         strcpy(pt, "Error file open. Press ESC to continue.");
  128.         window_puts(20, pt);
  129.     goto CopyExit;
  130.     }
  131.     FS = filelength( fid );
  132.     sprintf(fsiz, "%lu", FS);
  133.     len = _memmax();
  134.  
  135.     if ((buff = malloc(len)) == NULL) {
  136.         return 1;
  137.     }
  138.     window(20);
  139.     window_xy(20, 1, 20);
  140.     strcpy(pt, "COPY");
  141.     window_puts(20, pt);
  142.     window_xy(20, 2, 1);
  143.     strcpy(pt, "File name...................");
  144.     strcat(pt, p);
  145.     window_puts(20, pt);
  146.     window_xy(20, 3, 1);
  147.     strcpy(pt, "File size...................");
  148.     strcat(pt, fsiz);
  149.     window_puts(20, pt);
  150.     window_xy(20, 4, 1);
  151.     strcpy(pt, "Send count..................0");
  152.     window_puts(20, pt);
  153.  
  154.  
  155.     strcpy(pt, pa[1]);
  156.     pt[i = strlen(pt)] = 92;
  157.     pt[i+1] = 0x00;
  158.         strcat(pt, p);
  159.  
  160.  
  161.         if ( _dos_findfirst(pt,0xffff,&find))
  162.                 goto NEWF;
  163.         window(14);
  164.         window_xy(14, 1, 6);
  165.         window_puts(14, "File alredy exists.");
  166.         window_xy(14, 2, 3);
  167.         window_puts(14, "Do you wish to overwrite?");
  168.         window_xy(14, 3, 10);
  169.         window_puts(14, "Reply Y/N.");
  170.         window_xy(14, 3, 20);
  171.      ReIn:
  172.         ch = getch();
  173.         ch &= 0x00ff;
  174.         if ((ch == 'y')||(ch == 'Y')) {
  175.          deactivate( 14 );
  176.          goto NEWF;
  177.         }
  178.         else
  179.              if ((ch == 'n')||(ch == 'N')) {
  180.                 deactivate( 14 );
  181.                 window_xy(20, 6, 1);
  182.                 strcpy(pt, "Copy aborted!  Press ESC to continue.");
  183.                 window_puts(20, pt);
  184.                 goto CopyExit;
  185.              }
  186.              else
  187.                 goto ReIn;
  188.  
  189.         NEWF:
  190.     if ((fod = open(pt,O_WRONLY | O_CREAT | O_BINARY)) < 0) {
  191.               window(14);
  192.               window_xy(14, 2, 5);
  193.               window_puts(14, "Error File Open");
  194.               window_xy(14, 3, 4);
  195.               window_puts(14, "Press ESC to continue");
  196.               while ((ch = getch()) != ESC) ;
  197.               goto CopyExit;
  198.         }
  199.     count = 0;
  200.     while ((s = read(fid,buff,len)) != 0) {
  201.     /*  Copy this line */
  202.        count += s;
  203.        window_xy(20, 4, 29);
  204.        sprintf(fsiz, "%lu", count);
  205.        window_puts(20, fsiz);
  206.        write(fod,buff,s);
  207.     }
  208.     window_xy(20, 6, 1);
  209.     strcpy(pt, "Ready!  Press ESC to continue.");
  210.     window_puts(20, pt);
  211.  
  212. CopyExit:
  213.     while ((ch = getch()) != ESC) ;
  214.     deactivate(20);
  215.     close( fid );
  216.     close( fod );
  217.     free( buff );
  218.  
  219.     return (0);
  220. }
  221.  
  222.  
  223. /**************************************************************************/
  224. /*              ROCopy - copy file into remote LU                         */
  225. /**************************************************************************/
  226. ROCopy(char *p, int from)
  227.   {
  228.     unsigned char rts;
  229.     unsigned char wr;
  230.     char fsiz[10];
  231.     long count;
  232.     unsigned int ch;
  233.     int i;
  234.     unsigned int max_length ;
  235.     unsigned  char tpn[64];
  236.     struct rc {
  237.                 unsigned int prim;
  238.                 unsigned long sec;
  239.               } v_rc;
  240.     unsigned long rc;
  241.     char p_lu[9];
  242.     char p_mode[9];
  243.     int length;
  244.     char pt[80];
  245.     struct msg {  /*send_data*/
  246.                   int len;
  247.                   union dt{
  248.                       char str[MAX_PATH];
  249.                       struct{
  250.                              char rcw;
  251.                              unsigned long lncp;
  252.                             } inf;
  253.           } dt ;
  254.                 } gds;
  255.  
  256.               if (!tp_exist) {
  257.                                 tp_started
  258.                                             (lu_id,
  259.                                              tp_id,
  260.                                              &rc);
  261.                                 if (rc != 0) {
  262.                                                 return (-1);
  263.                                               }
  264.                                 tp_exist = 1; /* TP_started O.K! */
  265.                              }
  266.  
  267.               strcpy (tpn,"COPY "); /* name of work */
  268.               strcat (tpn, p );      /* name of file */
  269.               /*conv_id - return param -id work link */
  270.               strcpy(p_lu, pstr[from].plu);
  271.               strcpy(p_mode, pstr[from].mode_name);
  272.                    allocate  (p_lu,
  273.                               p_mode,
  274.                               tpn,
  275.                               WHEN_SESSION_ALLOCATED,
  276.                               NONE,
  277.                               NONE,
  278.                               NULL,
  279.                               NULL,
  280.                               tp_id,
  281.                               &conv_id,
  282.                               NULL,
  283.                               &v_rc) ;
  284.  
  285.  
  286.                   if (v_rc.prim != 0) {
  287.                                           return (-1);
  288.                                        }
  289.                   for (i=0;i<2;i++){
  290.  
  291.             memcpy(gds.dt.str, pa[i],strlen(pa[i])) ;
  292.                        gds.len =strlen(pa[i]+2);
  293.                        length=gds.len;
  294.                        send_data(tp_id,
  295.                                  conv_id,
  296.                                  &gds,
  297.                                  0,
  298.                                  0,
  299.                                  length,
  300.                                  &v_rc,
  301.                                  &rts);
  302.  
  303.                        if (v_rc.prim != 0) {
  304.                                           return (-1);
  305.                                             }
  306.                   }/* for */
  307.  
  308.                window(20);
  309.                window_xy(20, 1, 20);
  310.                strcpy(pt, "COPY");
  311.                window_puts(20, pt);
  312.                window_xy(20, 2, 1);
  313.                strcpy(pt, "File name...................");
  314.                strcat(pt, p);
  315.                window_puts(20, pt);
  316.                    max_length = 255;
  317.                    recwait (tp_id,
  318.                             conv_id,
  319.                             ll,
  320.                             &v_rc,
  321.                             &length,
  322.                             max_length,
  323.                             &rts,
  324.                             &gds,
  325.                             &wr);
  326.                   if (v_rc.prim != 0) {
  327.                                           return (-1);
  328.                                        }
  329.           if (gds.dt.inf.rcw == 'n') {
  330.                             window_xy(20, 6, 1);
  331.                             strcpy(pt, "Error file open. Press ESC to continue.");
  332.                             window_puts(20, pt);
  333.                             goto ROCExit;
  334.                    }
  335.           if (gds.dt.inf.rcw == 'e') {
  336. /*     file already exist    */
  337.                   window(14);
  338.                   window_xy(14, 1, 4);
  339.                   window_puts(14, "File alredy exists.");
  340.                   window_xy(14, 2, 1);
  341.                   window_puts(14, "Do you wish to overwrite?");
  342.                   window_xy(14, 3, 8);
  343.                   window_puts(14, "Reply Y/N.");
  344.                   window_xy(14, 3, 20);
  345.      ReIn:
  346.     ch = getch();
  347.         ch &= 0x00ff;
  348.         if ((ch == 'y')||(ch == 'Y')) {
  349.  
  350.             deactivate(14);
  351.             goto OW;
  352.         }
  353.         else
  354.              if ((ch == 'n')||(ch == 'N')) {
  355.                 deactivate( 14 );
  356.                   deallocate (tp_id,
  357.                               conv_id,
  358.                               Flush,
  359.                               &v_rc);
  360.                   if (v_rc.prim != 0) {
  361.                               return -1;
  362.                            }
  363.                   window_xy(20, 6, 1);
  364.                   strcpy(pt, "Copy aborted!  Press ESC to continue.");
  365.                   window_puts(20, pt);
  366.                   goto ROCExit;
  367.              }
  368.              else
  369.                 goto ReIn;
  370.       } /* file already exist */
  371.  
  372.       OW:
  373.          window_xy(20, 3, 1);
  374.          strcpy(pt, "File size...................");
  375.          sprintf(fsiz, "%lu", gds.dt.inf.lncp);
  376.      strcat(pt, fsiz);
  377.          window_puts(20, pt);
  378.          window_xy(20, 4, 1);
  379.          strcpy(pt, "Send count..................0");
  380.          window_puts(20, pt);
  381.          count = 0;
  382.  
  383.          RWC:
  384.                    max_length = 255;
  385.                    recwait (tp_id,
  386.                             conv_id,
  387.                             ll,
  388.                             &v_rc,
  389.                             &length,
  390.                             max_length,
  391.                             &rts,
  392.                             &gds,
  393.                             &wr);
  394.  
  395.                   if (v_rc.prim == deallocate_normal) {
  396.                              window_xy(20, 6, 1);
  397.                              strcpy(pt, "Ready!  Press ESC to continue.");
  398.                              window_puts(20, pt);
  399.                              goto ROCExit;
  400.                   }
  401.                   window_xy(20, 4, 29);
  402.                   count += gds.dt.inf.lncp;
  403.                   sprintf(fsiz, "%lu", count);
  404.           window_puts(20, fsiz);
  405.  
  406.                    max_length = 255;
  407.                    recwait (tp_id,
  408.                             conv_id,
  409.                             ll,
  410.                             &v_rc,
  411.                             &length,
  412.                             max_length,
  413.                             &rts,
  414.                             &gds,
  415.                             &wr);
  416.                   if (v_rc.prim != 0) {
  417.                                           return (-1);
  418.                                        }
  419.                   goto RWC;
  420.  
  421. ROCExit:
  422.     while ((ch = getch()) != ESC) ;
  423.     deactivate(20);
  424.     return 0;
  425. }
  426. /*****************************************************************************/
  427. /*              REM_CP - copy files into one remote LU                       */
  428. /*****************************************************************************/
  429. rem_cp ( p )
  430.   char *p; {
  431.     unsigned long FS; /* File Size */
  432.     int fod;
  433.     int fid;
  434.     char ptf[80];
  435.     char ptt[80];
  436.     char *buff; /* input buffer */
  437.     unsigned s;
  438.     unsigned long count;
  439.     unsigned len;
  440.     char fsize[10];
  441.     unsigned int max_length;
  442.     int length;
  443.     unsigned char rts;
  444.     unsigned char wr;
  445.     int  i;
  446.     struct rc {
  447.                 unsigned int prim;
  448.                 unsigned long sec;
  449.               } v_rc;
  450.     unsigned long rc;
  451.     struct msg {  /*send_data*/
  452.                   int len;
  453.           union dt {
  454.                       char str[MAX_PATH];
  455.                       struct{
  456.                              char rcw;
  457.                              unsigned long lncp;
  458.                             } inf;
  459.           } dt;
  460.                 } gds;
  461.  
  462.            max_length = 255;
  463.            recwait (tp_id,
  464.                     conv_id,
  465.                     ll,
  466.                     &v_rc,
  467.                     &length,
  468.                     max_length,
  469.                     &rts,
  470.                     &gds,
  471.                     &wr);
  472.           if (v_rc.prim != 0) {
  473.                                   return (-1);
  474.           }
  475.     memcpy(ptf,gds.dt.str,gds.len-2);
  476.     strcat(ptf,p+5);
  477.  
  478.            max_length = 255;
  479.            recwait (tp_id,
  480.                     conv_id,
  481.                     ll,
  482.                     &v_rc,
  483.                     &length,
  484.                     max_length,
  485.                     &rts,
  486.                     &gds,
  487.                     &wr);
  488.           if (v_rc.prim != 0) {
  489.                                   return (-1);
  490.           }
  491.     memcpy(ptt,gds.dt.str,gds.len-2);
  492.     strcat(ptt,p+5);
  493.  
  494.     if(( fid = open(ptf, O_RDONLY | O_BINARY))<0){
  495.          gds.dt.inf.rcw = 'n';
  496.          goto SD;
  497.        }
  498.     FS = filelength( fid );
  499.     gds.dt.inf.lncp = FS;
  500.  
  501.     if ( _dos_findfirst(ptt,0xffff,&find)){
  502.          gds.dt.inf.rcw = 'e';
  503.          goto SD;
  504.     } /* file already exist */
  505.  
  506.     gds.dt.inf.rcw = 'o';
  507.      SD:
  508.                gds.len =sizeof(struct msg)+2;
  509.                        length=gds.len;
  510.                        send_data(tp_id,
  511.                                  conv_id,
  512.                                  &gds,
  513.                                  0,
  514.                                  0,
  515.                                  length,
  516.                                  &v_rc,
  517.                                  &rts);
  518.  
  519.                        if (v_rc.prim != 0) {
  520.                                           return (-1);
  521.                                             }
  522.                    max_length = 255;
  523.                    recwait (tp_id,
  524.                             conv_id,
  525.                             ll,
  526.                             &v_rc,
  527.                             &length,
  528.                             max_length,
  529.                             &rts,
  530.                             &gds,
  531.                             &wr);
  532.  
  533.                   if (v_rc.prim == deallocate_normal) {
  534.  
  535.                                      tp_ended (tp_id, &rc);
  536.                                      return 0;
  537.                   }
  538.  
  539.  
  540.     len = _memmax();
  541.  
  542.     if ((buff = malloc(len)) == NULL) {
  543.        i = 1;
  544.        goto RMCExit;
  545.     }
  546.     if ((fod = open(ptt,O_WRONLY | O_CREAT | O_BINARY)) < 0) {
  547.               i = 1;
  548.               goto RMCExit;
  549.         }
  550.     count = 0;
  551.     while ((s = read(fid,buff,len)) != 0) {
  552.     /*  Copy this line */
  553.        count += s;
  554.        write(fod,buff,s);
  555.  
  556.                gds.dt.inf.lncp = count;
  557.            gds.len =sizeof(struct msg)+2;
  558.                length=gds.len;
  559.                send_data(tp_id,
  560.                          conv_id,
  561.                          &gds,
  562.                          0,
  563.                          0,
  564.                          length,
  565.                          &v_rc,
  566.                          &rts);
  567.  
  568.                 if (v_rc.prim != 0) {
  569.                                           i = 2;
  570.                                           goto RMCExit;
  571.                                      }
  572.     }  /* while */
  573.  
  574.       deallocate (tp_id,
  575.                   conv_id,
  576.                   Flush,
  577.                   &v_rc);
  578.       if (v_rc.prim != 0) {
  579.                               i = 2;
  580.                               goto RMCExit;
  581.                            }
  582.       tp_ended (tp_id, &rc);
  583.       i = 0;
  584.  
  585.       RMCExit:
  586.         if (i != 1) {
  587.                       close( fod );
  588.                       free( buff );
  589.                     }
  590.         close( fid );
  591.         if (i == 0)
  592.                 return 0;
  593.         return -1;
  594.   }
  595.  /***************************************************************************/
  596.